package com.lu.controller;

import com.lu.model.entity.SysMechantTypes;
import com.lu.model.response.Result;
import com.lu.service.SysMechantTypesService;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 商户分类 前端控制器
 * </p>
 *
 * @author zhanglu
 * @since 2022-09-19
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/sys-mechant-types")
public class SysMechantTypesController {

    private final SysMechantTypesService mechantTypesService;

    @PreAuthorize("hasAuthority('/sys-mechant-types/tree-nodes')")
    @GetMapping(value = "/tree-nodes")
    public Result<List<SysMechantTypes.TreeNodes>> treeNodes(@RequestParam(required = false) Long pid){
        return Result.success(mechantTypesService.getListByPid(pid));
    }

    @PreAuthorize("hasAuthority('/sys-mechant-types/add')")
    @PostMapping(value = "/add")
    public Result<SysMechantTypes> add(@RequestBody SysMechantTypes mechantTypes){
        return Result.success(mechantTypesService.add(mechantTypes));
    }

    @PreAuthorize("hasAuthority('/sys-mechant-types/update')")
    @PostMapping(value = "/update")
    public Result<SysMechantTypes> update(@RequestBody SysMechantTypes mechantTypes){
        return Result.success(mechantTypesService.updateSysMechantTypes(mechantTypes));
    }

    @PreAuthorize("hasAuthority('/sys-mechant-types/delete')")
    @PostMapping(value = "/delete/{id}")
    public Result<Integer> delete(@PathVariable Long id){
        boolean res = mechantTypesService.removeById(id);
        return res ? Result.success() : Result.error();
    }

    @PreAuthorize("hasAuthority('/sys-mechant-types/get')")
    @GetMapping(value = "/get/{id}")
    public Result<SysMechantTypes> getById(@PathVariable Long id){
        return Result.success(mechantTypesService.getById(id));
    }

}
